package com.source.baseData.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.baseData.entity.PoultryCategory;
import com.source.baseData.service.impl.PoultryCategoryServiceImpl;
import com.source.baseData.vo.PoultryCategoryVo;
import com.source.labelAndBarcode.entity.LabelingRecord;
import com.source.utils.CommonResult;
import com.source.utils.ExcelUtil;
import com.source.utils.MyPage;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 禽类品种前端控制器
 * </p>
 *
 * @author 枫情
 * @since 2021-02-06
 */
@Api(tags = "禽类品种基础数据管理")
@RestController
@RequestMapping("baseData/poultryCategory")
@CrossOrigin
public class PoultryCategoryController {

    @Autowired
    PoultryCategoryServiceImpl poultryCategoryService;

    @ApiOperation(value = "上传Excel，存储数据到数据库中")
    @PostMapping(value = "/uploadExcel", produces = "application/json;charset=utf-8")
    public CommonResult<String> uploadExcel(@RequestPart(value = "excelFile", required = false) MultipartFile excelFile) throws IOException {
        try {
            if (null != excelFile && !excelFile.isEmpty()) {
                ExcelUtil.uploadExcel(excelFile, PoultryCategory.class, poultryCategoryService);
                return new CommonResult<String>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), null);
            } else {
                return new CommonResult<String>(ResultEnum.PARAMS_NULL.getCode(), ResultEnum.PARAMS_NULL.getMsg(), null);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new CommonResult<String>(ResultEnum.ERROR, "上传异常！");
        }
    }

    @ApiOperation(value = "展示分页查询,参数pageCurrent，参数pageSize：每页的总记录数 默认1/10")
    @GetMapping("/findPageVo")
    public CommonResult<IPage<PoultryCategoryVo>> findPageVo(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<PoultryCategoryVo> page = new Page<>(current, size);
        IPage<PoultryCategoryVo> pageVo = poultryCategoryService.getPageVo(page);
        if (pageVo.getTotal() == 0) {
            return new CommonResult<IPage<PoultryCategoryVo>>(ResultEnum.DATA_NOT_FOUND.getCode(), ResultEnum.DATA_NOT_FOUND.getMsg(), null);
        } else {
            return new CommonResult<IPage<PoultryCategoryVo>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), pageVo);
        }
    }

    @ApiOperation(value = "根据poultryCategoryId进行单个查询（用于明细）")
    @GetMapping("/findById/{poultryCategoryId}")
    public CommonResult<PoultryCategoryVo> findOneVoById(@PathVariable("poultryCategoryId") String poultryCategoryId) {
        PoultryCategoryVo oneVo = poultryCategoryService.getOneVoById(poultryCategoryId);
        if (oneVo == null) {
            return new CommonResult<PoultryCategoryVo>(ResultEnum.DATA_NOT_FOUND.getCode(), ResultEnum.DATA_NOT_FOUND.getMsg(), null);
        } else {
            return new CommonResult<PoultryCategoryVo>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), oneVo);
        }
    }

    @ApiOperation(value = "根据主键id进行单个查询（用于修改禽类信息）")
    @GetMapping("/findPoultryById/{id}")
    public CommonResult<PoultryCategory> findPoultryById(@PathVariable String id){
        PoultryCategory grant = poultryCategoryService.getById(id);
        return new CommonResult<PoultryCategory>(ResultEnum.SUCCESS,grant);
    }

    @ApiOperation(value = "根据禽类品种编号进行查询")
    @GetMapping("/findByNumber/{poultryCategoryNumber}")
    public CommonResult<PoultryCategory> findByNumber(@PathVariable("poultryCategoryNumber") String poultryCategoryNumber) {
        QueryWrapper<PoultryCategory> wrapper = new QueryWrapper();
        wrapper.eq("poultryCategoryNumber", poultryCategoryNumber);
        PoultryCategory poultryCategory = poultryCategoryService.getOne(wrapper);
        if (poultryCategory == null) {
            return new CommonResult<PoultryCategory>(ResultEnum.DATA_NOT_FOUND.getCode(), ResultEnum.DATA_NOT_FOUND.getMsg(), null);
        } else {
            return new CommonResult<PoultryCategory>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), poultryCategory);
        }
    }

    @ApiOperation(value = "根据父类的级别编号，进行批量查询")
    @GetMapping("/findByParentId/{ParentId}")
    public CommonResult<Page<PoultryCategory>> findByParentId(@PathVariable("ParentId") String ParentId,
                                                              @RequestParam(value = "current", defaultValue = "1") int current,
                                                              @RequestParam(value = "size", defaultValue = "10") int size) {
        QueryWrapper<PoultryCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parentId", ParentId);
        List<PoultryCategory> poultryCategoryList = poultryCategoryService.list(wrapper);
        if (poultryCategoryList.isEmpty()) {
            return new CommonResult<Page<PoultryCategory>>(ResultEnum.DATA_NOT_FOUND.getCode(), ResultEnum.DATA_NOT_FOUND.getMsg(), null);
        } else {
            Page<PoultryCategory> page = MyPage.list2Page(current, size, poultryCategoryList);
            return new CommonResult<Page<PoultryCategory>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), page);
        }
    }

    @ApiOperation(value = "根据来源地进行模糊查询")
    @GetMapping("/findLikeOriginPlace")
    public CommonResult<Page<PoultryCategory>> findLikeOriginPlace(@RequestParam("OriginPlace") String OriginPlace,
                                                                   @RequestParam(value = "current", defaultValue = "1") int current,
                                                                   @RequestParam(value = "size", defaultValue = "10") int size) {
        QueryWrapper<PoultryCategory> wrapper = new QueryWrapper<>();
        wrapper.like("originPlace", OriginPlace);
        List<PoultryCategory> poultryCategories = poultryCategoryService.list(wrapper);
        if (poultryCategories.isEmpty()) {
            return new CommonResult<Page<PoultryCategory>>(ResultEnum.DATA_NOT_FOUND.getCode(), ResultEnum.DATA_NOT_FOUND.getMsg(), null);
        } else {
            Page<PoultryCategory> page = MyPage.list2Page(current, size, poultryCategories);
            return new CommonResult<Page<PoultryCategory>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), page);
        }
    }

    @ApiOperation(value = "根据来品种名称进行模糊查询")
    @GetMapping("/findLikeName")
    public CommonResult<Page<PoultryCategory>> findLikeName(@RequestParam("poultryCategoryName") String poultryCategoryName,
                                                            @RequestParam(value = "current", defaultValue = "1") int current,
                                                            @RequestParam(value = "size", defaultValue = "10") int size) {
        QueryWrapper<PoultryCategory> wrapper = new QueryWrapper<>();
        wrapper.like("poultryCategoryName", poultryCategoryName).or().likeRight("poultryCategoryName", poultryCategoryName).or().likeLeft("poultryCategoryName", poultryCategoryName);
        List<PoultryCategory> poultryCategories = poultryCategoryService.list(wrapper);
        if (poultryCategories.isEmpty()) {
            return new CommonResult<Page<PoultryCategory>>(ResultEnum.DATA_NOT_FOUND.getCode(), ResultEnum.DATA_NOT_FOUND.getMsg(), null);
        } else {
            Page<PoultryCategory> page = MyPage.list2Page(current, size, poultryCategories);
            return new CommonResult<Page<PoultryCategory>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), page);
        }
    }

    @ApiOperation(value = "添加一个禽类记录")
    @PostMapping("/save")
    public CommonResult<PoultryCategory> save(@RequestBody @Validated PoultryCategory poultryCategory) {
        try {
            poultryCategoryService.save(poultryCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<PoultryCategory>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), null);
        }
        return new CommonResult<PoultryCategory>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), poultryCategory);
    }

    @ApiOperation(value = "更新禽类记录")
    @PutMapping("/updateById")
    public CommonResult<PoultryCategory> updateById(@RequestBody @Validated PoultryCategory poultryCategory) {
        try {
            poultryCategoryService.updateById(poultryCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<PoultryCategory>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), null);
        }
        return new CommonResult<PoultryCategory>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), poultryCategory);
    }

    @ApiOperation(value = "根据id删除一个禽类记录")
    @DeleteMapping("/deleteById/{poultryCategoryId}")
    public CommonResult<Boolean> deleteById(@PathVariable("poultryCategoryId") String poultryCategoryId) {
        boolean b = poultryCategoryService.removeById(poultryCategoryId);
        if (b) {
            return new CommonResult<Boolean>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), true);
        } else {
            return new CommonResult<Boolean>(ResultEnum.DATA_NOT_FOUND.getCode(), ResultEnum.DATA_NOT_FOUND.getMsg(), false);
        }
    }

    @ApiOperation(value = "根据ids批量删除禽类记录")
    @DeleteMapping("/deleteByIds/{poultryCategoryIds}")
    public CommonResult<Boolean> deleteByIds(@PathVariable("poultryCategoryIds") List<String> poultryCategoryIds) {
        boolean b = poultryCategoryService.removeByIds(poultryCategoryIds);
        if (b) {
            return new CommonResult<Boolean>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), true);
        } else {
            return new CommonResult<Boolean>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), false);
        }

    }

    /*@ApiOperation(value = "下载excel")
    @GetMapping(value = "/downloadExcel", produces = "application/vnd.ms-excel;charset=utf-8")
    public void downloadExcel(HttpServletResponse response) throws IOException {
            ExcelUtil.downloadExcel(response, PoultryCategory.class, poultryCategoryService);
    }*/

    @ApiOperation(value = "多字段模糊查询")
    @GetMapping("/getVoByLike")
    public CommonResult<IPage<PoultryCategoryVo>> getVoByLike(@RequestParam(value = "current", defaultValue = "1") int current,
                                                              @RequestParam(value = "size", defaultValue = "10") int size,
                                                              @RequestParam("keyword") String keyword) {
        Page<PoultryCategoryVo> page = new Page<>();
        IPage<PoultryCategoryVo> voByLike = poultryCategoryService.getVoByLike(keyword, page);
        if (voByLike.getTotal() == 0) {
            return new CommonResult<IPage<PoultryCategoryVo>>(ResultEnum.DATA_NOT_FOUND.getCode(), ResultEnum.DATA_NOT_FOUND.getMsg(), null);
        } else {
            return new CommonResult<IPage<PoultryCategoryVo>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), voByLike);
        }
    }


}

